﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using dotUML.Core.Common;

namespace dotUML.Core.Parser
{
    /// <summary>
    /// Stereotypes parser class.
    /// </summary>
    class StereotypesXmlParser : XmlParserBase, IXmlParser
    {
        /// <summary>
        /// Parse a stereotypes block. And create stereotypes in a dotUML model.
        /// </summary>
        /// <param name="Xml">Xml block.</param>
        /// <param name="Model">dotUML model.</param>
        /// <returns>True if parsing was successful, otherwise - false.</returns>
        public override bool Parse(string Xml, IModel Model)
        {
            bool result = true;

            try
            {
                XElement stereotypes = XElement.Parse(Xml);
                IEnumerable<XElement> stereotypesList = stereotypes.Elements(Constants.Stereotype.StereotypeNode);
                foreach (XElement stereotype in stereotypesList)
                {
                    Model.AddStereotype(stereotype.Attribute(Constants.Common.NameAttribute).Value);
                }
            }
            catch (Exception ex)
            {
                result = false;
                NewError(EErrorType.Error, string.Format(Resources.StereotypesXmlParser_Parse_Error, ex.Message));
                // TODO: Log exception
            }

            return result;
        }
    }
}
